package service.impl;

import com.alibaba.fastjson.JSON;
import com.hjw.constants.TokenConstants;
import com.hjw.entity.LoginUser;
import com.hjw.service.TokenService;
import com.hjw.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Map;

public class TokenServiceImpl implements TokenService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    //1. 生成token信息
    @Override
    public Map<String, Object> createToken(LoginUser loginUser) {
        String token = loginUser.getToken();

        redisTemplate.opsForValue().set(TokenConstants.REDIS_PREFIX + token, JSON.toJSONString(loginUser));

        //1.1  定义数据map
        Map<String, Object> map = new HashMap<>();
        // map.put("username",loginUser.getUser().getUsername());
        // map.put("userId",loginUser.getUser().getId());
        map.put(TokenConstants.JWT_TOKEN,token);

        //2.2 将上面的数据map进行加密
        String jwtToken = JwtUtil.createToken(map);
        //2.3 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(TokenConstants.JWT_ACCESS_TOKEN,jwtToken);

        return resultMap;
    }
}
